package com.trio.gamerental.controller;

import com.trio.gamerental.model.User;
import com.trio.gamerental.pageModel.BaseHelper;
import com.trio.gamerental.pageModel.LoginHelper;
import com.trio.gamerental.service.UserService;
import com.trio.gamerental.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by 张俊强~ on 2016/10/26.
 * function:
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    public UserService userService;

    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String loginView() {

        return "/jsp/test/login.jsp";
    }


    @ResponseBody
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public LoginHelper userLogin(String loginEmail,String userPsw) {
        LoginHelper loginHelper;
//        System.out.println("用户名="+loginEmail+" 密码："+userPsw);
        userPsw= MD5Util.hash(userPsw);
        loginHelper= userService.judgeLogin(loginEmail,userPsw);
        return loginHelper;
    }

    @RequestMapping("/home")
    public String myHomePage(@RequestParam(value="email")String email,Model model) {
        if (email==null||email=="") return "";
        BaseHelper baseHelper=userService.judgeEmailExist(email);
        if(baseHelper.getSuccess()==true) return "";
        model.addAttribute("email", email);
        System.out.println("邮件="+email);
        return "/jsp/test/personalPage.jsp";
    }
    @RequestMapping("/message")
    public String myMessage(String email,Model model) {
        if (email==null||email=="") return "";
        BaseHelper baseHelper=userService.judgeEmailExist(email);
        if(baseHelper.getSuccess()==true) return "";
        model.addAttribute("email", email);
        return "/jsp/test/personalMessage.jsp";
    }
    @RequestMapping("/mynote")
    public String myNote(String email,Model model) {
        if (email==null||email=="") return "";
        BaseHelper baseHelper=userService.judgeEmailExist(email);
        if(baseHelper.getSuccess()==true) return "";
        model.addAttribute("email", email);
        return "/jsp/test/personalNotes.jsp";
    }

    /**
     * 验证邮箱是否重复
     * */

    @ResponseBody
    @RequestMapping(value = "/judgeemail",method = RequestMethod.POST)
    public BaseHelper judgeEmail(String loginEmail) {
        System.out.println("邮箱是："+loginEmail);

        BaseHelper baseHelper=userService.judgeEmailExist(loginEmail);
        return baseHelper;
    }

    @ResponseBody
    @RequestMapping(value = "/userregister",method = RequestMethod.POST)
    public BaseHelper userRegister(String userName,String loginEmail,String loginPsw) {
        User user=new User();
        user.setUserName(userName);
        user.setLoginEmail(loginEmail);
        loginPsw=MD5Util.hash(loginPsw);
        user.setLoginPsw(loginPsw);
        System.out.println("邮箱是："+user);
        BaseHelper baseHelper=userService.userRegister(user);
        return baseHelper;
    }
}
